package concurrencyPractice.chapter04;

import concurrencyPractice.GuardBy;
import concurrencyPractice.ThreadSafe;

import java.util.Collection;
import java.util.Collections;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 程序4-1 使用Java监视器模式的线程安全计数器
 * @date 2023/12/27 17:40:08
 */
@ThreadSafe
public class Counter {

    @GuardBy("this")
    private long value = 0;

    public synchronized long getValue() {
        return value;
    }

    public synchronized long increment() {
        if(value == Long.MAX_VALUE) {
            throw new IllegalStateException("counter overflow");
        }
        return ++ value;
    }
}
